classdef Interval
    %UNTITLED2 Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        imin % min bound of interval
        imax % max bound of interval
        name %string identify
    end
    
    methods
        
        function interval=Interval(imin, imax, name)
            if nargin==0
             return
            end
            
            if nargin==2
              interval.imin=imin;
              interval.imax=imax;
              return
            end
            
            if nargin==3
              interval.imin=imin;
              interval.imax=imax;
              interval.name=name;
              return
            end
            
        end
        
        function r=plus(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
             a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
              r=Interval(a+c,b+d, strcat(X.name,'+',Y.name));
        end
        
        function r=minus(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
            a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            r=Interval(a-d,b-c, strcat(X.name,'-',Y.name));
        end
        
        function r=mtimes(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
            a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            r=Interval(min([a*c,a*d,b*c,b*d]),max([a*c,a*d,b*c,b*d]), strcat(X.name,'*',Y.name));
             
        end
        
        function r=mrdivide(X,Y)
           X=Interval.prepare(X);
           Y=Interval.prepare(Y);
           a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
           r=Interval(min([a/c,a/d,b/c,b/d]),max([a/c,a/d,b/c,b/d]), strcat(X.name,'/',Y.name));
        end
        
        function r=max(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
			if(X>=Y)
				r=X;
				return;
			end
			if(X<=Y)
				r=Y;
				return;
			end
		
			
            
			a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            r=Interval(max(a,c), max(b,d), strcat('max(',X.name,Y.name,')'));
        end
         function r=min(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
			if(X>=Y)
				r=Y;
				return;
			end
			if(X<=Y)
				r=X;
				return;
			end
			
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
            a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            r=Interval(min(a,c), min(b,d), strcat('min(',X.name,Y.name,')'));
         end
        
         function C=lt(X,Y)
            X=Interval.prepare(X);
            Y=Interval.prepare(Y);
            a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            if b<c
                C=1;
                return;
            end
            C=0;
         end
         
         function C=gt(X,Y)
             X=Interval.prepare(X);
             Y=Interval.prepare(Y);
              a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
             if a>d
                C=1;
                return;
            end
            C=0;
         end
         
         function C=le(X,Y)
             X=Interval.prepare(X);
             Y=Interval.prepare(Y);
             a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
            if b<=c
                C=1;
                return;
            end
            C=0;
         end
         
         function C=ge(X,Y)
             X=Interval.prepare(X);
             Y=Interval.prepare(Y);
              a=X.imin;b=X.imax; c=Y.imin; d=Y.imax;
             if a>=d
                C=1;
                return;
            end
            C=0;
         end 
      
        function affine=convertToAffine(int)
            affine=AffineForm(int);
        end
        
       
    end
    
    
     methods (Static)
         function arr=convertToArray(intArray, mode)
             
            if nargin==0
             return
            end
            if nargin==1
             mode='min';
            end
           val_class=class(intArray);
			 if strcmp(val_class,'double')
                 arr=intArray;
                 return;
             end 
           size=length(intArray);
           arr=zeros(1,size);
           for i=1:size
               if(strcmp(mode,'min')==1)
                   arr(i)=intArray(i).imin;
               else
                   arr(i)=intArray(i).imax;
               end
           end
         end  
        
         function ret=minValue(intArray)
		     val_class=class(intArray);
			 if strcmp(val_class,'Interval')
                 arr=Interval.convertToArray(intArray,'min');
			     ret=min(arr);
             end
           
         end
         
         function inter=prepare(value)
             val_class=class(value);
             if strcmp(val_class,'Interval')
                inter=value;
                return
             end
             if strcmp(val_class,'double')
                inter=Interval(value, value, num2str(value));
                return
             end
              if strcmp(val_class,'AffineForm')
                inter=value.convertToInterval;
                return
             end
             
             
         end
         
         
     end
     
    
end

